package com.lzy.netty.websocket;

import com.lzy.enums.MsgTypeEnum;
import com.lzy.pojo.netty.ChatMsg;
import com.lzy.pojo.netty.DataContent;
import com.lzy.utils.JsonUtils;
import com.lzy.utils.LocalDateUtils;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author lzy
 * @date 2025/8/26 16:05
 * @description http处理类
 **/

// SimpleChannelInboundHandler<HttpObject> 泛型参数表示处理什么类型的数据 相当于入站
public class HeartBeatHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
       // 判断evt是否是IdleStateEvent（空闲事件状态,包含 读空闲/写空闲/读写空闲）
        if(evt instanceof IdleStateEvent){
            IdleStateEvent event = (IdleStateEvent) evt;
            if(event.state() == IdleState.READER_IDLE){
                //System.out.println("读空闲");
            }else if(event.state() == IdleState.WRITER_IDLE){
                //System.out.println("写空闲");
            }else if(event.state() == IdleState.ALL_IDLE){
                //System.out.println("chennel 关闭前,clients的数量为:" + ChatHandler.clients.size());
                //System.out.println("读写空闲");
                // 关闭无用的channel
                Channel channel = ctx.channel();
                channel.close();
                //System.out.println("chennel 关闭后,clients的数量为:" + ChatHandler.clients.size());
            }
        }
    }
}
